package com.demo.nio;

import com.demo.nio.client.Client;
import com.demo.nio.server.NioServer;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

/**
 * Created by hunter
 *
 * @Description NIO demo 顺带了解下底层调用原理
 */
public class MainTest {

    public static void main(String[] args) throws InterruptedException {
        NioServer server = new NioServer();
        new Thread(server).start();

        // 睡一下让客户端等待启动
        TimeUnit.SECONDS.sleep(3);

        new Thread(() -> {
            try {
                Client client = new Client("1号");
                client.startClient();
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        try {
                            client.sendMessage("测试消息 ");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }, 1000);

                // 保证线程不死
                while (true) ;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }
}
